package com.zyp.test.test;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {
    private boolean isVisible = true;
    private Button mButton;
    private int mWidthPixels;
    private float tx;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        mWidthPixels = displayMetrics.widthPixels;
        Button hide = (Button) findViewById(R.id.bt_hide);
        mButton = (Button) findViewById(R.id.bt);
        final TextView textView = (TextView) findViewById(R.id.tv);
        final Button translation = (Button) findViewById(R.id.bt_translation);
        tx = 100;
        final ObjectAnimator xAnimator = ObjectAnimator.ofFloat(mButton,"translationX",0,500);
        xAnimator.setDuration(1000);
        final ObjectAnimator yAnimator = ObjectAnimator.ofFloat(mButton,"translationY",0,500);
        final ObjectAnimator  rotationAnimator = ObjectAnimator.ofFloat(mButton,"rotation",0,-10,10,0,-10,0);
        rotationAnimator.setDuration(500);
        //rotationAnimator.setRepeatCount(ValueAnimator.INFINITE);
        final ObjectAnimator scaleXAnimation = ObjectAnimator.ofFloat(mButton,"scaleX",1f,1.2f);
       // scaleXAnimation.setRepeatCount(ValueAnimator.INFINITE);
        scaleXAnimation.setDuration(200);
        final ObjectAnimator scaleXAnimation1 = ObjectAnimator.ofFloat(mButton,"scaleX",1.2f,1f);
        //scaleXAnimation1.setRepeatCount(ValueAnimator.INFINITE);
        scaleXAnimation1.setDuration(200);
        final ObjectAnimator scaleYAnimation = ObjectAnimator.ofFloat(mButton,"scaleY",1f,1.2f);
       // scaleYAnimation.setRepeatCount(ValueAnimator.INFINITE);
        scaleYAnimation.setDuration(200);
        final ObjectAnimator scaleYAnimation1 = ObjectAnimator.ofFloat(mButton,"scaleY",1.2f,1f);
        //scaleYAnimation1.setRepeatCount(ValueAnimator.INFINITE);
        scaleYAnimation1.setDuration(200);
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(mButton,"alpha",1,0,1);
        yAnimator.setDuration(2000);
        final AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(scaleXAnimation).with(scaleYAnimation).before(rotationAnimator).before(scaleXAnimation1).with(scaleYAnimation1).after(800);
        //animatorSet.play(xAnimator).before(yAnimator).with(rotationAnimator).after(alphaAnimator);
        //animatorSet.setDuration(3000);
       //objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                animatorSet.start();
            }
        });
        xAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                float x = mButton.getX();
                float translationX = mButton.getTranslationX();
                Log.d("locationX",x+"");
                Log.d("locationTranslationX",translationX+"");
                
            }
        });
        translation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animatorSet.start();
                /*xAnimator.start();
                yAnimator.start();*/
            }
        });
       
        hide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isVisible){
                    mButton.setVisibility(View.INVISIBLE);
                    textView.setVisibility(View.INVISIBLE); 
                }else {
                    mButton.setVisibility(View.VISIBLE);
                    textView.setVisibility(View.VISIBLE);
                }
                
                isVisible = !isVisible;
            }
        });
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus){
            float x = mButton.getX();
            float translationX = mButton.getTranslationX();
            Log.d("locationX",x+"");
            Log.d("locationTranslationX",translationX+"");
        }
    }
}
